package com.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.hospital.entity.Equipment;
import com.hospital.service.intf.EquipmentService;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/equipment/")
public class EquipmentController {
    @Autowired
    private EquipmentService service;

    @PostMapping("add.do")
    public R add(Equipment equipment) {
        return service.add(equipment);
    }

    @GetMapping("dele.do")
    public R delectById(int id){
        return service.deleteById(id);
    }
    @PostMapping("upp.do")
    public R update(Equipment equipment){

        return service.update(equipment);
    }
    @GetMapping("page.do")
    public PageBean selectPage(String name,int page,int limit){
        return service.selectPage(name,page,limit);
    }


    //导出
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=equipment-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<Equipment> list=service.all();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),Equipment.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    //批量删除
    @PostMapping("deleids.do")
    public R deleids(int[] ids){
        return service.deleByIds(ids);
    }

    //批量添加
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.save(file);
    }

    @GetMapping("tj.do")
    public R DtoTj(){
        return service.onditionTj();
    }

}
